define(['jquery', 'bootstrap', 'agent', 'table', 'form'], function ($, undefined, Agent, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                queryParams: function (params) {
                    // 自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    //filter.后跟的是在ajax里使用的名称只需修改这两行
                    filter.shop_id = Config.shop_id;
                    //opop后跟的也是ajax里使用的名称，后面是条件
                    op.shop_id = '=';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    return params;
                },
                extend: {
                    index_url: 'shop_goods/index',
                    add_url: 'shop_goods/add',
                    multi_url: 'shop_goods/multi',
                    table: 'shop_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: false,
                columns: [
                    [
                        {field: 'goods_id', title: 'ID', sortable: true,operate: false},
                        {field: 'goods.cover_img', title: '封面图', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'goods.goods_name', title: '商品名称', operate: 'LIKE'},
                        {field: 'market_price', title: '市场价',sortable: true,operate: false},
                        // {field: 'price', title: '销售价格',sortable: true,operate: false},
                        {field: 'cost', title: '成本',sortable: true,operate: false},
                        {field: 'status', title: '商品状态', formatter: Table.api.formatter.toggle ,searchList: {1: '开启', 0: '关闭'}},
                        {field: 'audit_status', title: '审核状态', formatter: Table.api.formatter.status, custom:{0: '待审核', 1: '审核通过',2:'审核不通过'},searchList: {0: '待审核', 1: '审核通过',2:'审核不通过'}},
                        {field: 'createtime', title: '添加时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});